using System.Linq;
using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CommodityTypeController : ControllerBase
    {
        public IRepository<CommodityType> _commodityTypeRepository;

        public readonly IConfiguration _configuration;

        public CommodityTypeController(IRepository<CommodityType> commodityTypeRepository, IConfiguration configuration)
        {
            _commodityTypeRepository = commodityTypeRepository;
            _configuration = configuration;
        }
        //添加分类
        [HttpPost]
        public ActionResult AddComType(ParamCommodityType model)
        {
            var addComType = new CommodityType
            {
                TypeName = model.TypeName
            };
            _commodityTypeRepository.Insert(addComType);
            return Ok(new
            {
                Code = 1000,
                Data = addComType,
                Msg = "获得成功"
            });
        }
        //编辑分类
        [HttpPut("{id}")]
        public ActionResult UpdateType(int id, ParamCommodityType model)
        {
            var Type = _commodityTypeRepository.GetById(id);
            if (Type != null)
            {

                Type.TypeName = model.TypeName;
                _commodityTypeRepository.Update(Type);
                return Ok(new
                {
                    Code = 1000,
                    Data = Type,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = " ",
                Msg = "抱歉，未找到"
            });
        }
        //获得所有分类
        [HttpGet]
        public ActionResult GetAny()
        {
            var TypeAny = _commodityTypeRepository.Table;
            return Ok(new
            {
                Code = 1000,
                Data = TypeAny,
                Msg = "获得成功"
            });
        }
    }
}